package com.crocodile.game17.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.crocodile.game17.common.AppException;
import com.crocodile.game17.common.AppExceptionMsgEnum;
import com.crocodile.game17.common.LoginUser;
import com.crocodile.game17.mapper.UserMapper;
import com.crocodile.game17.pojo.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
@Slf4j
@Service
public class UserDetailServiceImpl implements UserDetailsService {

    @Autowired
    private UserMapper userMapper;


    /**
     * 登录认证
     * @param username
     * @return
     * @throws UsernameNotFoundException
     */
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {

//        数据库查询
        User user = userMapper.getByUsername(username);

        if(ObjectUtil.isNull(user)){
            throw new AppException(AppExceptionMsgEnum.NOT_FOUND);
        }

//        todo 权限查询

        //封装返回
        LoginUser loginUser = new LoginUser();
        loginUser.setUser(user);

        return loginUser;
    }
}
